﻿using System;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace notepad
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string errMsg = "1、检测到配置文件出错\r\n2、请重新选择需要替换的notepad路径\r\n3、或者在C:\\Windows\\notepad.ini文件中配置notepad的路径";
            string configPath = "C:\\Windows\\notepad.ini";
            if (!File.Exists(configPath)) {
                try {
                    File.Create(configPath);
                    MessageBox.Show(errMsg);
                }
                catch(Exception ex)
                {
                    MessageBox.Show("请以管理员身份运行");
                }
              
            }
            else
            {
                try
                {
                    //读取路径
                    StringBuilder path = new StringBuilder();
                    using (FileStream fs = new FileStream(configPath, FileMode.Open, FileAccess.Read))
                    {
                        byte[] bt = new byte[1024];

                        while (fs.Read(bt, 0, bt.Length) > 0)
                        {
                            string txt = Encoding.Default.GetString(bt);
                            path.AppendLine(txt);
                        }

                    }
                    if (args.Length > 0)
                    {
                        //打开程序
                        System.Diagnostics.Process.Start(path.ToString(), "\"" + args[0] + "\"");
                    }
                    else
                    {
                        System.Diagnostics.Process.Start(path.ToString());
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(errMsg);
                }
            }
           
        }
    }
}
